package org.example.order.controller.front;

import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.example.order.bo.ConfirmOrderBo;
import org.example.order.service.OrderService;
import org.example.order.vo.OrderConfirmVo;
import org.example.order.vo.OrderRespVo;
import org.example.order.vo.OrderRestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/oms/front/order")
public class OrderFrontController {

    @Autowired
    private OrderService orderService;

    // 订单页需要的数据
    @PostMapping("/toTrade")
    public R<?> toTrade(@Validated @RequestBody ConfirmOrderBo confirmOrderBo) {
        OrderConfirmVo confirmVo = orderService.confirmOrder(confirmOrderBo);
        return R.ok(confirmVo);
    }

    // 创建订单
    @PostMapping("/create")
    public R<?> create(@Validated @RequestBody OrderRestVo orderRestVo){
        return orderService.createOrder(orderRestVo);
    }

    // 查询订单状态
    @GetMapping("/status/{orderSn}")
    public R<Integer> status(@PathVariable("orderSn") String orderSn){
        Integer status = orderService.queryOrderStatus(orderSn);
        return R.ok(status);
    }

    // 查询订单信息
    @GetMapping("/info/{id}")
    public R<OrderRespVo> queryOrderInfo(@PathVariable("id") Long id){
        OrderRespVo orderRespVo = orderService.queryOrderInfo(id);
        return R.ok(orderRespVo);
    }

    // 用户查询订单
    @GetMapping("/member")
    public R<?> queryMemberOrder(@RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("orderStatus") Integer status){
        PageUtils memberOrderVos = orderService.queryMemberOrder(pageNum, pageSize, status);
        return R.ok(memberOrderVos);
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("/delete/{orderId}")
    public R<?> delete(@PathVariable("orderId") Long orderId){
        return orderService.deleteOrder(orderId)>0? R.ok():R.error();
    }

    /**
     * 确定收货
     * @return
     */
    @PutMapping("/update/confirm/{orderId}")
    public R<?> confirmGoods(@PathVariable("orderId") Long orderId){
        return orderService.updateConfirm(orderId)>0? R.ok():R.error();
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel/{orderId}")
    public R<?> cancel(@PathVariable("orderId") Long orderId){
        return orderService.cancelOrder(orderId)>0? R.ok():R.error();
    }
}
